package momo.app.comms;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import momo.app.structure.Message;


public class RcvLoopback extends Thread
{
	final static int timeToWait = 30;
	private boolean interrupted;
	private LinkedBlockingQueue<String> queue;
	
	public RcvLoopback()
	{
		queue = new LinkedBlockingQueue<String>();
		this.start();
	}
	
	public void run()
	{
		while(!interrupted)
		{
			try
			{
				String data = queue.poll(timeToWait, TimeUnit.SECONDS);
				
				if ( data != null )
					rcvMsg(data);
				
			}catch (InterruptedException ie)
			{
				ie.printStackTrace();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void rcvMsg(String data)
	{
		Message msg = Message.createMessage(data);
		CommsManager.getInstance().rcvMsg(msg);
	}
	
	public void put(String data)
	{
		try
		{
			queue.put(data);
		}catch(InterruptedException ie)
		{
			ie.printStackTrace();
		}
	}
}//end of class RcvLoopback